<?php
class ZList{
	public $QUERY 	= '';
	public $TOTAL   = '';
	public $WHERE 	= array();
	public $ORDER 	= array();
	public $HAVING 	= array();
	public $LIMIT 	= array();	
	public $VARS 	= array();
	
	public $defaultORDER  = array();
	public $defaultWHERE  = array();
	public $defaultHAVING = array();
	public $keyCompareStatic = array();
	public $reField		= array();
	
	public $fieldWHERE = array();
	public $fieldORDER = array();

	public function get( $k, $method='REQUEST' ){
		if( $method == "POST" ){
			$method = $_POST;
		}else if( $method == "GET" ){
			$method = $_GET;
		}else{
			$method = $_REQUEST;
		}		
		
		return addslashes( stripslashes($method[$k]) );
	}
	
	public function _initWhere(){
		$array_compare_symbols = search_key_map();
		
		$array_where = $this->fieldWHERE;
		
		foreach( $array_where as $field ){
			if( preg_match("/`[\w-_]+`\.`(.*)`/i",$field,$match)){
				$k = $match[1];
			}else{
				$k = str_replace('`','',$field);
			}
			
			if( empty($_REQUEST[$k]) ){ 
				continue;
			}
			
			$v = $this->get($k,'REQUEST');
			$c = 'ct_'.$k;
			//check type of compare, default is equal
			if( 
				isset( $_REQUEST[$c] ) && array_key_exists( $_REQUEST[$c], $array_compare_symbols )
			){
				$t = $this->get($c,'REQUEST');			
			}else{
				$t = 'equal';
			}

			if( array_key_exists( $field, $this->keyCompareStatic )){
				$t = strtolower($this->keyCompareStatic[$field]); 
			}			
			
			$this->VARS[$k] = $v;
			$this->VARS[$c] = $t;
			
			if( isset($this->reField[$field])){
				$field = $this->reField[$field];
			}
						
			$this->WHERE[] = join_sql_search( $field,$t, $v );
		}
	}

	public function _initOrder(){
		foreach( $this->fieldORDER as $k ){
			if( !isset($_POST['sort_'.$k]) && !isset($_GET['sort_'.$k])){
				continue;
			}
			
			$s = $this->get('sort_'.$k) == 'desc' ? 'desc' : 'asc';
			$this->ORDER[$k] = strtoupper($s);
			$this->VARS['sort_'.$k] = $s;
		}
	}
	
	public function setWhere( $str ){
		if( !is_array( $str) )
			$str = explode(',',$str);
		foreach( $str as $f ){
			$this->fieldWHERE[] = $f;
		}
	}
	
	public function setKeyCompare( $field, $key_label){
		$this->keyCompareStatic[$field] = strtolower($key_label);
	}

	public function remakeField( $new_field, $concat ){
		$this->reField[$new_field] = $concat; 		
	}
	
	public function setOrder( $str ){
		if( !is_array( $str) )
			$str = explode(',',$str);
		foreach( $str as $f ){
			$this->fieldORDER[] = $f;
		}
	}

	public function setDefaultOrder( $k,$df='ASC' ){
		if( is_array( $k ) ){
			foreach($k as $t => $d ){
				$this->defaultORDER[$t] = $d;
			}
			return $this;
		}
		$this->defaultORDER[$k] = $df;
	}

	public function setDefaultWhere( $arr ){
		if( is_array( $arr ) ){
			foreach($arr as $d ){
				$this->defaultWHERE[] = $d;
			}
			return $this;
		}	
		$this->defaultWHERE[] = $arr;
	}

	public function setDefaultHaving( $arr ){
		if( is_array( $arr ) ){
			foreach($arr as $d ){
				$this->defaultHAVING[] = $d;
			}
			return $this;
		}	
		$this->defaultHAVING[] = $arr;
	}
	
	public function setGroupBy( $str ){
		$this->GROUPBY = $str;
	}

	public function setLimit( $str ){
		$this->LIMIT = $str;
	}
	
	public function setVars( $k, $str ){
		$this->VARS[$k] = $str;
	}
	
	public function getVars(){
		return $this->VARS;	
	}

	public function getQueryVars( $more =''){
		$vars = array();
		foreach( $this->VARS as $k =>$v ){
			$vars[] = "$k=$v";
		}
		return 	count($vars) > 0 ? implode('&',$vars).$more : '';	
	}
	
	public function getQuery( $total = false ){
		$this->_initWhere();
		$this->_initOrder();
		
		$orders = $this->defaultORDER;
		foreach( $this->ORDER as $k => $v ){
			$orders[$k] = $v;
		}
		$orders = array_unique($orders);
		
		$wheres = $this->WHERE;
		
		foreach( $this->defaultWHERE as $k => $v ){
			$wheres[] = $v;
		}	
			
		$wheres = array_unique($wheres);	
		
		$havings = $this->defaultHAVING;
		
		foreach( $this->HAVING as $k => $v ){
			$havings[] = $v;
		}
		$havings = array_unique($havings);
		
		$qr_where = count($wheres) > 0 ? ' WHERE '.implode(' AND ', $wheres) : '';
		$qr_having = count($havings) > 0 ? ' HAVING '.implode(' AND ', $havings) : '';
		$qr_order = array(); 
		
		foreach( $orders as $k => $v ){
			$qr_order[] = " ".$k." ".$v;
		}
		
		$qr_order = count($qr_order) > 0 ? ' ORDER BY '.implode(', ', $qr_order) : '';
		if( $total )
			return $this->TOTAL.$qr_where.$qr_having;
		$qr = $this->QUERY.$qr_where.$qr_having;

		if( $this->GROUPBY  ){
			$qr.=' GROUP BY '.$this->GROUPBY;
		}

		$qr.=$qr_order;

		if( $this->LIMIT  ){
			$qr.=' LIMIT '.$this->LIMIT;
		}

		return $qr;
	}
}
?>